﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WebApiMIT3.Database;
using WebApiMIT3.Models;
using WebApiMIT3.Utils;

namespace WebApiMIT3.BusinessLayer
{
    public class DataManager
    {
        public async Task<string> GetNews(List<string> categoryList)
        {
            var dataModelList = new List<DataModel>();
            var dataModelReturnList = new List<DataModel>();
            var sqlManager = new SQLManager();
            dataModelList = await sqlManager.GetModelFromDatabase();

            dataModelReturnList = await this.CheckDataModelListForCategories(dataModelList, categoryList);

            return XMLHandler.PackModelIntoXML(dataModelReturnList);
        }

        public async Task PostNews(string path)
        {
            var sqlManager = new SQLManager();
            var files = new DirectoryInfo(path).GetFiles("*.*", SearchOption.TopDirectoryOnly);

            string result = await this.GetFilesFromDirectory(files);

            var dataModelList = XMLHandler.UnpackModelFromXML(result);
            var category = new CategoryService();

            foreach (var dataModel in dataModelList)
            {
                if (!dataModel.Categories.Any())
                {
                    await category.FindCategory(dataModel);
                }

                await sqlManager.AddModelToDatabase(dataModel);
            }
        }

        private async Task<string> GetFilesFromDirectory(FileInfo[] files)
        {
            var result = string.Empty;

            if (files != null)
            {
                if (files.Any())
                {
                    foreach (var item in files)
                    {
                        using (StreamReader sr = new StreamReader(item.FullName))
                        {
                            string line = await sr.ReadToEndAsync();
                            result = line;
                        }
                    }
                }
            }

            return result;
        }

        private async Task<List<DataModel>> CheckDataModelListForCategories(List<DataModel> dataModelList, List<string> categoryList)
        {
            var result = new List<DataModel>();
            await Task.Run(() =>
            {
                if (categoryList.Any())
                {
                    foreach (var category in categoryList)
                    {
                        foreach (var dataModel in dataModelList)
                        {
                            if (dataModel.Categories != null && dataModel.Categories.Contains(category) && !result.Contains(dataModel))
                            {
                                result.Add(dataModel);
                            }
                        }
                    }
                }
                else
                {
                    result.AddRange(dataModelList);
                }
            });
            return result;
        }
    }
}